package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.think.teaching.mapper.PaperQuestionMapper;
import com.think.teaching.model.ExamPaper;
import com.think.teaching.model.PaperQuestion;
import com.think.teaching.model.Question;
import com.think.teaching.service.PaperQuestionService;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 试卷和试题关联 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class PaperQuestionServiceImpl extends BaseServiceImpl<PaperQuestionMapper, PaperQuestion> implements PaperQuestionService {

	@Override
	public PaperQuestion AddNewPaperQuestion(PaperQuestion paperQuestion) {
		if (this.save(paperQuestion)) {
			return paperQuestion;
		}
		return null;
	}


	@Override
	public PaperQuestion UpdatePaperQuestion(PaperQuestion paperQuestion) {
		if (this.updateById(paperQuestion)) {
			return paperQuestion;
		}
		return null;
	}

	@Override
	public PaperQuestion SetPaperQuestionState(Integer id, Integer state) {
		UpdateWrapper<PaperQuestion> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public List<Question> ListPaperQuestion(Boolean withoutBan) {
		//TODO:获取当前试卷的所有试题
		return null;
	}

	@Override
	public PaperQuestion GetPaperQuestionInfo(Integer id) {
		QueryWrapper<PaperQuestion> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	public List<LinkedHashMap<String,Object>> GetPaperQuestionByZone(Integer id){
		return  baseMapper.GetPaperQuestionByZone(id);
	}

	public Boolean CheckExisted(Integer questionId,Integer zoneId){
		QueryWrapper<PaperQuestion> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("zone_id", zoneId).eq("question_id", questionId);
		return getOne(queryWrapper) != null;
	}
	
	/**
	 * 删除和指定的courseId相关的试卷
	 * @param courseId
	 * @return
	 */
	public Boolean DeleteRecordsByQuestionId(Integer questionId) {
		UpdateWrapper<PaperQuestion> queryWrapper = new UpdateWrapper<PaperQuestion>();
		queryWrapper.eq("question_id", questionId).set("delflag", 1);
		return this.remove(queryWrapper);
	}
	
}
